package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //添加套餐信息还要添加关联表SetmealDish表的信息
    /**
     * 添加套餐信息
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("添加套餐信息和关联表的信息{}",setmealDTO);
        setmealService.saveSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询信息{}",setmealPageQueryDTO);
        PageResult pageResult= setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        log.info("根据id查询套餐{}",id);
        SetmealVO setmealVO=setmealService.selectById(id);
        return Result.success(setmealVO);
    }


    /**
     * 修改套餐信息
     * @param setmealVO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealVO setmealVO){
        log.info("修改套餐信息{}",setmealVO);
        setmealService.update(setmealVO);
        return Result.success();
    }

    /**
     * 套餐起售停售,
     *  停售没事
     *  起售的时候要先判断,该套餐id所有的dishid对应的菜品是否为在售状态
     *  如果有一个菜品停售就不能将套餐起售
     */
    /**
     * 根据id改变套餐的状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStopSetMeal(@PathVariable Integer status,Long id){
        log.info("根据id改变套餐的状态status:{},id:{}", status,id);
        setmealService.startOrStopSetMeal(status,id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(Long[] ids){
        log.info("批量删除套餐:ids:{}", ids);
        setmealService.delete(ids);
        return Result.success();
    }

}
